package com.gmail.rborovyk.brightness_widget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;

final class Presets {
	private static final String TAG = "Presets";
	
	static final String PREFS_NAME = "prefs";
	static final String PREFS_RISE_BRIGHTNESS = "rise";
	static final String PRESET_COUNT = "c";
	static final String PRESET_VALUE = "v";
	static final String PRESET_NAME = "n";
	
	//Support for Pre API8
	static final boolean HAS_AUTO_MODE = (Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR_MR1); //only API 8+
	static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";
	static final int SCREEN_BRIGHTNESS_MODE_MANUAL = 0;
	static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;
	
	private SharedPreferences mPrefs;
	private ArrayList<Preset> mPresets = new ArrayList<Preset>();
	private boolean mRiseBrightness;
	private boolean mModified;
	
	static final class Preset {
		String name;
		int value;
		
		Preset(String name, int value) {
			this.name = name;
			this.value = value;
		}

		@Override
		public String toString() {
			return name+" ("+(value*100/255)+"%)";
		}
	}
	
	public Presets(Context context) {
		mPrefs = context.getSharedPreferences(PREFS_NAME, 0);
		readPresets();
	}
	
	public int getPresetsCount() {
		return mPresets.size();
	}
	
	public Preset getPreset(int index) {
		return mPresets.get(index);
	}
	
	public boolean getRiseBrightness() {
		return mRiseBrightness;
	}
	
	public void setRiseBrightness(boolean rise) {
		mRiseBrightness = rise;
		mModified = true;
	}
	
	public Collection<Preset> getAllPresets() {
		return mPresets;
	}
	
	public void updatePreset(int index, String name, int value) {
		Preset p = mPresets.get(index);
		p.name = name;
		p.value = value;
		Collections.sort(mPresets, mComparator);
		mModified = true;
	}
	
	public void addPreset(String name, int value) {
		mPresets.add(new Preset(name, value));
		Collections.sort(mPresets, mComparator);
		mModified = true;
	}
	
	public void removePreset(int index) {
		mPresets.remove(index);
		Collections.sort(mPresets, mComparator);
		mModified = true;
	}
	
	private void readPresets() {
		try{
			mRiseBrightness = mPrefs.getBoolean(PREFS_RISE_BRIGHTNESS, true);
			mPresets.clear();
			int cnt = mPrefs.getInt(PRESET_COUNT, 0);
			for(int idx = 0; idx < cnt; ++idx) {
				mPresets.add(new Preset(
						mPrefs.getString(PRESET_NAME+idx, "!"), 
						mPrefs.getInt(PRESET_VALUE+idx, 0)));
			}
		}catch(ClassCastException ex) {
			mPresets.clear();
			Log.d(TAG, "Error reading presets. /n"+ex.getStackTrace());
		}
		
		mModified = false;
	}
	
	public void writePresets() {
		if(!mModified)
			return;
		
		SharedPreferences.Editor prefs = mPrefs.edit();
		prefs.clear();
		prefs.commit();
				
		int cnt = mPresets.size();
		for(int idx = 0; idx < cnt; ++idx) {
			prefs.putInt(PRESET_VALUE+idx, mPresets.get(idx).value);
			prefs.putString(PRESET_NAME+idx, mPresets.get(idx).name);
		}
		prefs.putInt(PRESET_COUNT, cnt);
		prefs.putBoolean(PREFS_RISE_BRIGHTNESS, mRiseBrightness);
		prefs.commit();
	}
	
	
	Comparator<Preset> mComparator = new Comparator<Preset>() {
		@Override
		public int compare(Preset p1, Preset p2) {
			return p1.value - p2.value;
		}
	};
}
